package com.ycrlkj.android.modulescore.main.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.ycrlkj.android.modulescore.main.R;
import com.ycrlkj.android.modulescore.main.apiservice.XxStationApiService;
import com.ycrlkj.android.modulescore.main.databinding.ActivityHomeBinding;
import com.ycrlkj.android.modulescore.main.databinding.ActivityXxStationDetailBinding;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.exception.ErrorConsumer;
import com.ycrlkj.android.modulespublic.common.net.transformer.ResponseTransformer;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;
import com.ycrlkj.android.modulespublic.common.widget.titlebar.MTitleBar;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
@Route(path = RoutePath.PAGE_XX_STATION_DETAIL)
public class XxStationDetailActivity extends AppCompatActivity {

    @Autowired
    String id;

    @Inject
    XxStationApiService xxStationApiService;

    private ActivityXxStationDetailBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);

        if (id == null && id.length() != 19) {
            XPopupUtil.showAlert(this, "未找站点信息, 关闭页面", new OnConfirmListener() {
                @Override
                public void onConfirm() {
                    finish();
                }
            });
        }

        binding = ActivityXxStationDetailBinding.inflate(getLayoutInflater());
        binding.setLifecycleOwner(this);

        setContentView(binding.getRoot());

        initData();
        initTitleBar();
    }

    public void initTitleBar(){
        MTitleBar titleBar = binding.mTitleBar;
        titleBar.setBtnEvents(new MTitleBar.BtnEvents() {
            @Override
            public void setBtnLeftOnClickListener() {
                finish();
            }

            @Override
            public void setBtnRightOnClickListener() {

            }
        });
    }


    @SuppressLint("CheckResult")
    public void initData() {
        xxStationApiService.getById(id)
                .compose(ResponseTransformer.obtain())
                .subscribe(xxStation -> {
                        binding.setXxStation(xxStation);
                        },
                        new ErrorConsumer() {
                            @Override
                            protected void error(ApiException e) {

                            }
                        });

    }


}